package com.sl.plat.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sl.common.ResponseData;
import com.sl.water.entity.PlanStatic;
import com.sl.water.service.AttachService;
import com.sl.water.service.PlanStaticService;
import com.sl.water.vo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @description:静态预案接口
 * @author: duan
 * @time: 2025-06-27 14:46
 */

@RestController
@RequestMapping("/plat/plan/static")
@CrossOrigin
@RequiredArgsConstructor
@Slf4j
@Tag(name = "预案-静态预案", description = "静态预案")
public class PlanStaticController {
    private final AttachService attachService;
    private final PlanStaticService planStaticService;

    @PostMapping
    @Operation(summary = "新增")
    public ResponseData save(@RequestBody PlanStaticIVO planStaticIVO) {
        PlanStatic planStatic = BaseVO.convert(planStaticIVO, PlanStatic.class);
        planStaticService.save(planStatic);
        return ResponseData.success(planStatic.getId());
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除")
    public ResponseData removeById(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id) {
        planStaticService.removeById(id);
        return ResponseData.success("删除成功");
    }

    @PutMapping("/{id}")
    @Operation(summary = "修改")
    public ResponseData updateById(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id,
            @RequestBody PlanStaticIVO planStaticIVO) {
        PlanStatic planStatic = BaseVO.convert(planStaticIVO, PlanStatic.class);
        planStatic.setId(id);
        planStaticService.updateById(planStatic);
        return ResponseData.success("修改成功");
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询")
    public ResponseData<PlanStatic> get(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id) {
        return ResponseData.success(planStaticService.getById(id));
    }

    @GetMapping("/pageList")
    @Operation(summary = "分页列表")
    public ResponseData<Page<PlanStatic>> pageList(
            @Parameter(
                    description = "页码",
                    example = "1", required = true
            )
            @RequestParam(defaultValue = "1") int page,
            @Parameter(
                    description = "分页数量",
                    example = "10", required = true
            )
            @RequestParam(defaultValue = "10") int size,
            @Parameter(
                    description = "名称(模糊查询)"
            )
            @RequestParam(required = false) String name,
            @Parameter(
                    description = "发文单位"
            )
            @RequestParam(required = false) String fwdw,
            @Parameter(
                    description = "年度"
            )
            @RequestParam(required = false) String year
    ) {
        Page<PlanStatic> planStaticPage = planStaticService.pageList(page, size,name,fwdw,year);
        return ResponseData.success(planStaticPage);
    }

    @PostMapping("/reportUpload")
    @Operation(summary = "报告上传")
    @ApiResponse(responseCode = "200", description = "上传成功", content = @Content(schema = @Schema(implementation = AttachVO.class)))
    public ResponseEntity<?> upload(
            @Parameter(description = "文件", required = true)
            @RequestPart(required = true)
                    MultipartFile file) {
        return attachService.upload("plan_static", file);
    }
}
